package com.xinjian.jianba.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xinjian.jianba.dto.VideoDto;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;
@Component
public final class AliVideoUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(AliVideoUtil.class);

	
	private static String accessKeyId ;
	
	private static String accessKeySecret;
	
	@Value("${jianba.vod.accessKeyId}")
	public  void setAccessKeyId(String accessKeyId) {
		AliVideoUtil.accessKeyId = accessKeyId;
	}
	@Value("${jianba.vod.accessKeySecret}")
	public  void setAccessKeySecret(String accessKeySecret) {
		AliVideoUtil.accessKeySecret = accessKeySecret;
	}

	private static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
		//点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域
		String regionId = "cn-shanghai";
		DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		DefaultAcsClient client = new DefaultAcsClient(profile);
		return client;

	}

	/*获取播放地址函数*/
	private static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String videoId) throws Exception {
		GetPlayInfoRequest request = new GetPlayInfoRequest();
		request.setVideoId(videoId);
		return client.getAcsResponse(request);
	}

	public static VideoDto getVideoInfo(String videoId) {
		VideoDto dto = new VideoDto();
		getPlayAuth(dto, videoId);
		getPlayUrl(dto, videoId);
		return dto;
	}

	private static void getPlayUrl(VideoDto dto, String videoId) {
		DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
		GetPlayInfoResponse response = new GetPlayInfoResponse();
		try {
			response = getPlayInfo(client, videoId);
			List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
			//播放地址
			for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
				String url = playInfo.getPlayURL();
				LOGGER.info("PlayInfo.PlayURL = " + url);
				if (StringUtils.isNotBlank(url)) {
					if (url.lastIndexOf(".m3u8") > 0) {
						dto.setPlayUrl(url);
					} else if (url.lastIndexOf(".mp4") > 0) {
						dto.setMp4Url(url);
					}

				}
			}
			//Base信息
			LOGGER.info("VideoBase.Title = " + response.getVideoBase().getTitle());
			LOGGER.info("getPlayUrl RequestId = " + response.getRequestId());
		} catch (Exception e) {
			LOGGER.info("ErrorMessage = " + e.getLocalizedMessage());
			throw new BusinessException(ResponseCode.ERROR, "获取视频播放地址异常!");
		}
	}

	/*获取播放凭证函数*/
	private static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client, String videoId) throws Exception {
		GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
		request.setVideoId(videoId);
		return client.getAcsResponse(request);
	}

	private static void getPlayAuth(VideoDto dto, String videoId) {
		DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
		GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
		try {
			response = getVideoPlayAuth(client, videoId);
			//播放凭证
			dto.setPlayAuth(response.getPlayAuth());
			LOGGER.info("PlayAuth = " + response.getPlayAuth());
			//VideoMeta信息
			LOGGER.info("VideoMeta.Title = " + response.getVideoMeta().getTitle());
			LOGGER.info("RequestId = " + response.getRequestId());
		} catch (Exception e) {
			LOGGER.info("ErrorMessage = " + e.getLocalizedMessage());
			throw new BusinessException(ResponseCode.ERROR, "获取视频播放凭证异常!");
		}
	}
	/*删除视频函数*/
	public static String deleteVideo(String videoIds) {
		DefaultAcsClient client=initVodClient(accessKeyId, accessKeySecret);
		DeleteVideoRequest request = new DeleteVideoRequest();
	    //多个用逗号分隔，最多支持20个
//	    request.setVideoIds("视频ID1,视频ID2");
	    request.setVideoIds(videoIds);
	    DeleteVideoResponse response=null;
	    try {
			  response=client.getAcsResponse(request);
		} catch (Exception e) {
			LOGGER.info("删除视频失败:ErrorMessage = " + e.getLocalizedMessage());
			throw new BusinessException(ResponseCode.ERROR, "ErrorMessage = " + e.getLocalizedMessage());
		} 
	    LOGGER.info("删除视频成功:"+response.getRequestId());
	    return "SUCCESS";
	}
}
